package com.inifitness.common.tools;

import java.util.Map;
import java.util.UUID;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.slf4j.MDC;

/**
 * 日志跟踪工具类
 *
 * @author sunjinfeng
 * @version 1.0
 * @since 2024/1/22 11:18
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class MdcUtil {

  public static final String TRACE_ID = "traceId";

  public static String generateTraceId() {
    String traceId = UUID.randomUUID().toString().replace("-", "");
    MDC.put(TRACE_ID, traceId);
    return traceId;
  }

  public static String getTraceId() {
    return MDC.get(TRACE_ID);
  }

  public static void setTraceId(String traceId) {
    MDC.put(TRACE_ID, traceId);
  }

  public static void setContextMap(Map<String, String> context) {
    MDC.setContextMap(context);
  }

  public static void removeTraceId() {
    MDC.remove(TRACE_ID);
  }

  public static void clear() {
    MDC.clear();
  }
}
